package com.szqhyxj.youxiangju.biz.onlineChannel;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;

import java.util.List;

import butterknife.BindBitmap;
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * XiaoPanYu
 * on 2018/11/26
 */
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.IconViewHolder> {

    private List<ChannelModel.RowsItem> rows;

    private IconClickListener iconClickListener;
    private boolean isBind;
    private OnItemClickListener onRecyclerViewItemClickListener;


    private int thisPosition;


    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }


    public ChannelAdapter(List<ChannelModel.RowsItem> rows, IconClickListener iconClickListener) {
        this.rows = rows;
        this.iconClickListener = iconClickListener;
    }

    @NonNull
    @Override
    public IconViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new IconViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_channel_item, viewGroup, false));
    }

    public void setOnRecyclerViewItemClickListener(OnItemClickListener onItemClickListener) {
        this.onRecyclerViewItemClickListener = onItemClickListener;
    }

    //再定义一个int类型的返回值方法
    public int getthisPosition() {
        return thisPosition;
    }

    public void setThisPosition(int thisPosition) {
        this.thisPosition = thisPosition;
    }
    Boolean isFirst = false;

    @Override
    public void onBindViewHolder(@NonNull IconViewHolder iconViewHolder, int i) {
        ChannelModel.RowsItem rowitem = rows.get(i);

        iconViewHolder.channelName.setText(rowitem.getChannelName() + "");
        String amountConfig = "单笔最低" + rowitem.getMinAmount() + "元，单笔最高" + rowitem.getMaxAmount() + "元";
        iconViewHolder.amountconfig.setText(amountConfig);
        int startTime = rowitem.getStartTime();
        int endTime = rowitem.getEndTime();
        String start;
        String end;
        if (num(startTime)) {
            start = "0" + startTime + ":00";
        } else {
            start = startTime + ":00";
        }

        if (num(endTime)) {
            end = "0" + endTime + ":00";
        } else {
            end = endTime + ":00";
        }
        String time = start + "-" + end;
        iconViewHolder.time.setText("交易时间：" + time);
        Log.e("时间", iconViewHolder.time.getText().toString());
        if (rowitem.getSupport() == 0) {
            iconViewHolder.item.setBackgroundResource(R.mipmap.bzhichi);
            iconViewHolder.errorArea.setVisibility(View.GONE);
        } else {

            if (rowitem.getBindStatus() == 0 || rowitem.getBindStatus() == 1 || rowitem.getBindStatus() == 2) {
                iconViewHolder.item.setBackgroundResource(R.mipmap.weibangd);
                iconViewHolder.errorArea.setVisibility(View.GONE);


            }
            if (rowitem.getBindStatus() == 3) {
                iconViewHolder.item.setBackgroundResource(R.mipmap.chuliz);
                iconViewHolder.errorArea.setVisibility(View.GONE);


            }
            if (rowitem.getBindStatus() == 4) {
                iconViewHolder.item.setBackgroundResource(R.mipmap.yibangd);
                iconViewHolder.errorArea.setVisibility(View.GONE);


            }
            if (rowitem.getBindStatus() == -1 || rowitem.getBindStatus() == -2) {
                iconViewHolder.item.setBackgroundResource(R.mipmap.weibangd);
                iconViewHolder.errorArea.setVisibility(View.VISIBLE);
                iconViewHolder.errortip.setText(rowitem.getErrMsg());


            }

        }

        if (iconViewHolder != null) {
            iconViewHolder.channelName.setText(rowitem.getChannelName() + "");
            if (i == getthisPosition()) { //選中的顔色就設成了
                if (isFirst==false){
                    iconViewHolder.choose.setBackgroundResource(R.mipmap.weixuanzhong);
                    isFirst  = true;
                }else{
                    iconViewHolder.choose.setBackgroundResource(R.mipmap.xuanzhong);
                }
            } else {
                //未選中的顔色 就設成了 白色
                iconViewHolder.choose.setBackgroundResource(R.mipmap.weixuanzhong);
            }
        }
        if (onRecyclerViewItemClickListener != null) { //點擊事件
            iconViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRecyclerViewItemClickListener.onClick(i);
                    iconClickListener.onClickItem(i, rowitem);
                }
            });
            iconViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onRecyclerViewItemClickListener.onLongClick(i);
                    return false;
                }
            });
        }



    }

    private Boolean num(int time) {
        boolean b = false;
        if (0 < time && time < 10) {
            b = true;
        } else if (time > 10) {
            b = false;
        }
        return b;
    }


    @Override
    public int getItemCount() {
        return rows.size();
    }

    static class IconViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.channelName)
        TextView channelName;
        @BindView(R.id.amountconfig)
        TextView amountconfig;
        @BindView(R.id.erroricon)
        TextView erroricon;
        @BindView(R.id.errortip)
        TextView errortip;
        @BindView(R.id.item)
        LinearLayout item;
        @BindView(R.id.errorArea)
        LinearLayout errorArea;
        @BindView(R.id.time)
        TextView time;

        @BindColor(R.color.textcolor4e)
        int textcolor4e;
        @BindColor(R.color.policytext)
        int gren;
        @BindColor(R.color.textgreen)
        int green;

        @BindBitmap(R.mipmap.hui)
        Bitmap hui;
        @BindView(R.id.choose)
        TextView choose;

        public IconViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface IconClickListener {
        void onClickItem(int i, ChannelModel.RowsItem rows);
    }


}
